//
//  HotTripViewController.m
//  RoadLine
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 RedFireNoodles. All rights reserved.
//

#import "HotTripViewController.h"
#import "UIImageView+WebCache.h"
#import "AFHTTPRequestOperationManager.h"
#import "HotTripCell.h"
#import "Trip.h"
#import "MJRefresh.h"
#import "TripViewController.h"
// 定义宏
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface HotTripViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *hotTripArr;
@property(nonatomic, assign)NSInteger page;


@end


@implementation HotTripViewController

// 初始化
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hotTripArr = [NSMutableArray array];
        self.title = @"热门日记";
            }
   
    return self;
    
}


// 数据处理
-(void)data{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSInteger number = self.page * 20;
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/hot/?start=%ld&count=20",number];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey: @"trips"];
        for (NSMutableDictionary *dic in arr) {
            Trip *hotTrip = [[Trip alloc] init];
            [hotTrip setValuesForKeysWithDictionary:dic];
            [self.hotTripArr addObject:hotTrip];
            [hotTrip release];
        }
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.tableView.footer endRefreshing];
     }];

    self.page++;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = NO;
    self.page = 0;
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [self data];
    }];
    
    [self.tableView.footer beginRefreshing];
    [_tableView release];
    //修改UInavigationBar的字体颜色(系统API没有这个设置 只能自己建一个Label覆盖 然后修改label的属性)
    UILabel *titleLabelPrice = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabelPrice.font = [UIFont boldSystemFontOfSize:18];
    titleLabelPrice.textColor = [UIColor whiteColor];
    titleLabelPrice.backgroundColor = [UIColor clearColor];
    titleLabelPrice.text = @"   热门日记";
    self.navigationItem.titleView = titleLabelPrice;
    [titleLabelPrice release];
}

// 日记tableView必须实现的系统方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.hotTripArr.count;
    
    
}
// 日记tableView必须实现的系统方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    HotTripCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[HotTripCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuse] autorelease];
    }
    Trip *trip = self.hotTripArr[indexPath.row];
    NSURL *url = [NSURL URLWithString:trip.cover_image];
    cell.titleLabel.text = trip.name;
    cell.tripDayLabel.text = [NSString stringWithFormat:@"%ld天", trip.day_count];
    [cell.tripImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"BEIJING.png"]];
    cell.placeLabel.text = trip.popular_place_str;

    
    
    return cell;
}

// 设置cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return 180;

}


// 热门游记详情显示
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TripViewController *tripVC = [[TripViewController alloc] init];
    [self.navigationController pushViewController:tripVC animated:YES];
    Trip *trip = [[Trip alloc] init];
    trip = self.hotTripArr[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%@",trip.trip_id];
    tripVC.highQualityTripModelId = str;
    tripVC.htmlLatsStr = @"trackpoints";
    [tripVC autorelease];
    
    
}

// 设置白天模式
- (void)setLightDayModel {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:246/255.0 green:85/255.0 blue:91/255.0 alpha:1];
    
}

// 设置夜间模式
- (void)setNightDayModel {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:4/255.0 green:165/255.0 blue:171/255.0 alpha:1];
}


- (void)dealloc{

    [_hotTripArr release];
    [_tableView release];
    [super dealloc];

}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
